package com.c2b.algorithm.leetcode.base;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * 两个数组的交集
 *
 * @author c2b
 * @since 2022/5/19 15:00
 */
public class LC0349 {

    public int[] intersection(int[] nums1, int[] nums2) {
        ArrayList<Integer> objects = new ArrayList<>();
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        int index1 = 0;
        int index2 = 0;
        while (index1 < nums1.length && index2 < nums2.length) {
            if (nums1[index1] == nums2[index2] && !objects.contains(nums1[index1])) {
                objects.add(nums1[index1]);
                index1++;
                index2++;
            } else {
                if (nums1[index1] > nums2[index2]) {
                    index2++;
                } else {
                    index1++;
                }
            }
        }
        int[] ints = new int[objects.size()];
        for (int i = 0; i < objects.size(); i++) {
            ints[i] = objects.get(i);
        }
        return ints;
    }

}
